<?php

namespace app\index\controller;
use think\App;
use think\Db;
use app\common\model\SelfReport;

class Index extends Base
{
    public function __construct(App $app = null)
    {
        parent::__construct($app);
    }

    // 旧电视赠品
    public function giftContent()
    {
            $data = \db('product')->where('deduction','=',1)->value('content');
//          $data =  explode('+',$data);
            $this->s_msg('',$data);
    }


    /**
     * 首页内容
     */
    public function index()
    {
        $us_type = $this->user['us_type'];

        //返回商品分类
        // $data_categories = model('cate')->order('id ','desc')->field('id,ca_name,ca_pic')->select();
        //返回平台介绍信息
        $self = model('SelfReport')->field('title,summary,pic,content')->find();
        // 旧电视图片
        $pic = db('cate')->where('id', '=', 8)->value('ca_pic');
        //新闻快讯
        $new = model('News')->where('type','=','0')->field('title')->select();
        //返回轮播图
        $data_carousel_pic = model('News')->where('type','=','1')->field('title,pic')->select();


//
//        foreach ($data_carousel_pic as $k => $value) {
//            $data_carousel_pic[$k] = explode(',', $value['value']);
//        }

        $data = ['pic' => $data_carousel_pic, 'self' => $self, 'news' => $new, 'd_pic' => $pic,'us_type'=>$us_type];
        $this->s_msg('', $data);
    }

    /**
     * 商品分类内容
     */
    public function member()
    {
        $id = input('post.id');
        if ($id ==2){
            $data = model('product')
                ->where('ca_id', $id)
                ->where('pd_status', 2)
                ->where('type',1)
                ->field('id,type,pd_name,pd_price,pd_head_pic,content,gong_id')
                ->select();
            if (count($data)==0){
                $this->e_msg('暂无商品',0);
            }
            foreach ($data as $k=>$v){
                $data[$k]['gong_name'] = model('admin')->where('id',$v['gong_id'])->value('ad_name');
            }
            $data1 = model('product')
                ->where('ca_id', $id)
                ->where('pd_status', 2)
                ->where('type',2)
                ->field('id,type,pd_name,pd_price,pd_head_pic,content,gong_id')
                ->select();
            if (count($data1)==0){
                $data1 = [];
            }
            $dataInfo =  ['commodity'=>$data,'gift'=>$data1];
            $this->s_msg('', $dataInfo);
        }else{
            $data = model('product')
                ->where('ca_id', $id)
                ->where('pd_status', 2)
                ->where('type',1)
                ->field('id,type,pd_name,pd_price,pd_head_pic,content,gong_id')
                ->select();
            if (count($data)==0){
                $this->e_msg('暂无商品',0);
            }
            foreach ($data as $k=>$v){
                $data[$k]['gong_name'] = model('admin')->where('id',$v['gong_id'])->value('ad_name');
            }
            $data1 = model('product')
                ->where('ca_id', $id)
                ->where('pd_status', 2)
                ->where('type',2)
                ->field('id,type,pd_name,pd_price,pd_head_pic,content,gong_id')
                ->select();
            if (count($data1)==0){
                $data1 = [];
            }
            $dataInfo =  ['commodity'=>$data,'gift'=>$data1];

            $this->s_msg('', $dataInfo);
        }

    }

    /**
     * 商品详情
     */
    public function product()
    {
        $id = input('post.id');
        $data = model('product')
            ->where('id', $id)
            ->find();
//        halt($data);
        $aa = explode(',', $data['pd_pic']);
        $bb = explode('+', $data['pd_content']);

        foreach ($aa as $k => $v) {
            //  $data[$k]['pd_pic'] = stripslashes(',',explode(',' , $v['pd_pic']));
            $abc[$k] = $v;
        }
        foreach ($bb as $k => $v) {
            //  $data[$k]['pd_pic'] = stripslashes(',',explode(',' , $v['pd_pic']));
            $bbb[$k] = $v;
        }
        $data['pd_pic'] = $abc;
        $data['pd_detail'] = $bbb;
        $this->s_msg('', $data);
    }

    /**
     * 平台介绍
     */
    public function pt()
    {
        $id = input('post.id');
        $data = model('SelfReport')
            ->where('id', $id)
            ->field('content')
            ->find();
        $this->s_msg('', $data);
    }


}